//
// Copyright (C) 2011 - 2013  Steve Channell steve.channell@cepheis.com
//
// This file is part of Cephei.QL, an open-source library wrapper 
// arround QuantLib http://quantlib.org/
//
// Cephei.QL is open source software: you can redistribute it and/or modify it
// under the terms of the license.  You should have received a
// copy of the license along with this program; if not, please email
// <support@cepheis.com>. The license is also available online at
// <http://cepheis.com/license.htm>.
//
// This program is distributed in the hope that it will be useful, but WITHOUT
// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
// FOR A PARTICULAR PURPOSE.  See the license for more details.
//
// Version 2.2 with QuantLib 1.2.1
//#include "stdafx.h"
#include "HimalayaMultiPathPricer.h"
using namespace Cephei::QL::Experimental;
#include <gen/QL/Payoff.h>
#include <gen/QL/Methods/Montecarlo/MultiPath.h>
using namespace Cephei::QL;
using namespace Cephei::QL::Methods::Montecarlo;
#undef HANDLE
#undef ABSTRACT
#undef STRUCT
Cephei::QL::Experimental::CHimalayaMultiPathPricer::CHimalayaMultiPathPricer (Cephei::QL::IPayoff^ payoff, Double discount) 
{
	_pSpinlock = new boost::detail::spinlock ();
    CPayoff^ _Cpayoff;
    try
    {
#ifdef HANDLE
        _phHimalayaMultiPathPricer = NULL;
#endif
        _Cpayoff = safe_cast<CPayoff^> (payoff);
        _Cpayoff->Lock();
        boost::shared_ptr<QuantLib::Payoff>& _payoff = static_cast<boost::shared_ptr<QuantLib::Payoff>&> (_Cpayoff->GetShared ()); 
        QuantLib::DiscountFactor _discount = (QuantLib::DiscountFactor)ValueHelper::Convert (discount); //d
        _ppHimalayaMultiPathPricer = new boost::shared_ptr<QuantLib::HimalayaMultiPathPricer> (new QuantLib::HimalayaMultiPathPricer ( _payoff,  _discount ));
        
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
        if (_Cpayoff != nullptr) _Cpayoff->Unlock();
    }
}
Cephei::QL::Experimental::CHimalayaMultiPathPricer::CHimalayaMultiPathPricer (boost::shared_ptr<QuantLib::HimalayaMultiPathPricer>& childNative, Object^ owner) 
{
	_pSpinlock = new boost::detail::spinlock ();
#ifdef HANDLE
	_phHimalayaMultiPathPricer = NULL;
#endif
	_ppHimalayaMultiPathPricer = &childNative;
    
}
Cephei::QL::Experimental::CHimalayaMultiPathPricer::CHimalayaMultiPathPricer (QuantLib::HimalayaMultiPathPricer& childNative, Object^ owner) 
{
#ifdef HANDLE
	_phHimalayaMultiPathPricer = NULL;
#endif
	_ppHimalayaMultiPathPricer = new boost::shared_ptr<QuantLib::HimalayaMultiPathPricer> (&childNative);
    
    _HimalayaMultiPathPricerOwner = owner;
    
}

Cephei::QL::Experimental::CHimalayaMultiPathPricer::CHimalayaMultiPathPricer (CHimalayaMultiPathPricer^ copy) 
{
	_pSpinlock = new boost::detail::spinlock ();
#ifdef HANDLE
	_phHimalayaMultiPathPricer = NULL;
#endif
	if (copy->HasNative() != NULL)
    {
		_ppHimalayaMultiPathPricer = new boost::shared_ptr<QuantLib::HimalayaMultiPathPricer> (copy->GetShared());
        
    }
}
Cephei::QL::Experimental::CHimalayaMultiPathPricer::CHimalayaMultiPathPricer (PLATFORM::Type^ t) 
{
	_pSpinlock = new boost::detail::spinlock ();
#ifdef HANDLE
	_phHimalayaMultiPathPricer = NULL;
#endif
	if (!t->IsSubclassOf(CHimalayaMultiPathPricer::typeid))
		throw REFNEW Exception ("Invalid base-case init");
}
#ifdef HANDLE
Cephei::QL::Experimental::CHimalayaMultiPathPricer::CHimalayaMultiPathPricer (QuantLib::Handle<QuantLib::HimalayaMultiPathPricer>& childNative, Object^ owner)  
{
	_pSpinlock = new boost::detail::spinlock ();
	_phHimalayaMultiPathPricer = &childNative;
	_ppHimalayaMultiPathPricer = &static_cast<boost::shared_ptr<QuantLib::HimalayaMultiPathPricer>>(childNative.currentLink());
    
    _HimalayaMultiPathPricerOwner = owner;
}
Cephei::QL::Experimental::CHimalayaMultiPathPricer::CHimalayaMultiPathPricer (QuantLib::Handle<QuantLib::HimalayaMultiPathPricer> childNative)  
{
	_pSpinlock = new boost::detail::spinlock ();
	_phHimalayaMultiPathPricer = &childNative;
	_ppHimalayaMultiPathPricer = &static_cast<boost::shared_ptr<QuantLib::HimalayaMultiPathPricer>>(childNative.currentLink());
    
}
#endif
#ifdef STRUCT
Cephei::QL::Experimental::CHimalayaMultiPathPricer::CHimalayaMultiPathPricer (QuantLib::HimalayaMultiPathPricer childNative)  
{
	_pSpinlock = new boost::detail::spinlock ();
#ifdef HANDLE
	_phHimalayaMultiPathPricer = NULL;
#endif
	_ppHimalayaMultiPathPricer = new boost::shared_ptr<QuantLib::HimalayaMultiPathPricer> (new QuantLib::HimalayaMultiPathPricer (childNative));
    
}
#endif

Cephei::QL::Experimental::CHimalayaMultiPathPricer::~CHimalayaMultiPathPricer ()
{
	if (_pSpinlock != NULL)
    {
        delete _pSpinlock;
        _pSpinlock = NULL;
    }
    if (_ppHimalayaMultiPathPricer != NULL)
    {
	    delete _ppHimalayaMultiPathPricer;
        _ppHimalayaMultiPathPricer = NULL;
    }
}
Cephei::QL::Experimental::CHimalayaMultiPathPricer::!CHimalayaMultiPathPricer ()
{
	if (_pSpinlock != NULL)
    {
        delete _pSpinlock;
    }
    if (_ppHimalayaMultiPathPricer != NULL)
    {
	    delete _ppHimalayaMultiPathPricer;
    }
}
QuantLib::HimalayaMultiPathPricer& Cephei::QL::Experimental::CHimalayaMultiPathPricer::GetReference ()
{
    if (_ppHimalayaMultiPathPricer == NULL) throw REFNEW NativeNullException ();
	return **_ppHimalayaMultiPathPricer;
}
boost::shared_ptr<QuantLib::HimalayaMultiPathPricer>& Cephei::QL::Experimental::CHimalayaMultiPathPricer::GetShared ()
{
    if (_ppHimalayaMultiPathPricer == NULL) throw REFNEW NativeNullException ();
	return *_ppHimalayaMultiPathPricer;
}
QuantLib::HimalayaMultiPathPricer* Cephei::QL::Experimental::CHimalayaMultiPathPricer::GetPointer ()
{
    if (_ppHimalayaMultiPathPricer == NULL) throw REFNEW NativeNullException ();
	return &**_ppHimalayaMultiPathPricer;
}
#ifdef HANDLE
QuantLib::Handle<QuantLib::HimalayaMultiPathPricer>& Cephei::QL::Experimental::CHimalayaMultiPathPricer::GetHandle ()
{
	if (_phHimalayaMultiPathPricer == NULL)
	{
		_phHimalayaMultiPathPricer = new Handle<QuantLib::HimalayaMultiPathPricer> (*_ppHimalayaMultiPathPricer);
	}
	return *_phHimalayaMultiPathPricer;
}
#endif
bool Cephei::QL::Experimental::CHimalayaMultiPathPricer::HasNative () 
{
	return (_ppHimalayaMultiPathPricer != NULL);
}

//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Factory class

Cephei::QL::Experimental::IHimalayaMultiPathPricer^ Cephei::QL::Experimental::CHimalayaMultiPathPricer_Factory::Create (Cephei::QL::IPayoff^ payoff, Double discount)
{
    return REFNEW CHimalayaMultiPathPricer ( payoff,  discount);
}
